package com.wthink.newMain.stationStudy

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.ImageView
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.wthink.card.models.GMBInfoBundle
import com.wthink.hzgj.R
import com.wthink.hzgj.databinding.ActivityStationStudyBinding
import com.wthink.hzgj.station.data.Station
import com.wthink.hzgj.util.adapter.BaseViewHolder
import com.wthink.hzgj.util.adapter.OnItemClickListener
import com.wthink.hzgj.util.closeDefaultAnimator
import com.wthink.newMain.newBusiness.adapter.BusinessHistotyAdapter
import com.wthink.newMain.stationStudy.adapter.StationStudyAdapter
import com.wthink.newMain.stationStudy.viewmodel.StationStudyViewmodel
import com.wthink.newMain.view.changeLine.ChangeLineDialogAdapter
import kotlinx.android.synthetic.main.item_change_line_dialog.view.*

class StationStudyActivity :AppCompatActivity(){

    lateinit var binding: ActivityStationStudyBinding
    private val viewModel by viewModels<StationStudyViewmodel>()

    var isUir = true
    var spinnerItem = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_station_study)
        binding.lifecycleOwner = this
        binding.viewModel = viewModel
        init()
    }

    private fun init() {
        binding.back.setOnClickListener {
            finish()
        }

        binding.upLine.isSelected = true
        binding.downLine.isSelected = false
        binding.upLine.setOnClickListener {
            binding.upLine.isSelected = true
            binding.downLine.isSelected = false
            isUir = true
            viewModel.findLineName(spinnerItem,isUir)
        }
        binding.downLine.setOnClickListener {
            binding.upLine.isSelected = false
            binding.downLine.isSelected = true
            isUir = false
            viewModel.findLineName(spinnerItem,isUir)
        }

        binding.btnImport.setOnClickListener {
            viewModel.importStation()
        }
        binding.btnExport.setOnClickListener {
            viewModel.exportStation()
        }

        viewModel.start()

        binding.lineSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                spinnerItem = viewModel.listLine.value!![position]
                viewModel.findLineName(spinnerItem,isUir)
            }
        }

        binding.lineRecyler.layoutManager = LinearLayoutManager(this,
            LinearLayoutManager.VERTICAL,false)
/*
        binding.lineRecyler.adapter = StationStudyAdapter().apply {
            mItemClickListener = object : OnItemClickListener {
                override fun onItemClick(obj: Any, position: Int) {
                    viewModel.select(obj as Station)
                }
            }
        } 0 ·1  
*/
        binding.lineRecyler.adapter = object : StationStudyAdapter() {
            override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
                super.onBindViewHolder(holder, position)
                holder.itemView.setOnClickListener {
                    viewModel.select(viewModel.stationList.value!![position])
                }

                holder.itemView.findViewById<ImageView>(R.id.icon_fresh).setOnClickListener {
                    viewModel.loadStation(viewModel.stationList.value!![position])
                }
            }
        }
    }
}